<!doctype html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">
  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>
  <link rel="import" href="dynamic-imports/dynamic-element.html">
</head>
<body>

<dynamic-element></dynamic-element>

<script>

  suite('dynamic imports', function() {

    test('use importHref to load and create an element', function(done) {
      var d = document.querySelector('dynamic-element');
      d.whenDynamicContentReady(function() {
        assert(d._readied, true, 'dynamic element not readied');
        assert.ok(d.$.content, 'dynamic element does not have content');
        assert(d.$.outer._readied, true, 'dynamic sub-element not readied');
        assert.ok(d.$.outer.$.content, 'dynamic sub-element does not have content');
        assert(d.$.outer.$.inner._readied, true, 'dynamic sub-element not readied');
        assert.ok(d.$.outer.$.inner.$.content, 'dynamic sub-element does not have content');
        done();
      });
    });

    suite('async/sync loading', function() {

      var url = 'dynamic-imports/dynamic-element.html';

      test('importHref sync loads by default', function(done) {
        Polymer.Base.importHref(url, function(e) {
          assert.isFalse(e.target.hasAttribute('async'),
                        'sync load is default');
          done();
        });
      });

      test('importHref sync called again, triggers load', function(done) {
        Polymer.Base.importHref(url, function() {
          Polymer.Base.importHref(url, function() {
            done();
          });
        });
      });

      test('importHref async loading', function(done) {
        Polymer.Base.importHref('dynamic-imports/async-import.html', function(e) {
          assert.isTrue(window.asyncImportLoaded, 'flag not set from import');
          assert.isTrue(e.target.hasAttribute('async'), 'async load');
          assert.ok(e.target.import);
          done();
        }, null, true);
      });

      test('importHref does not leak event listeners on load', function(done) {
        var errorSpy = sinon.spy();
        var loadSpy = sinon.spy(function(e) {
          var target = e.target;
          target.dispatchEvent(new Event('error'));
          assert.isFalse(errorSpy.called, 'doesn\'t trigger the error event listener');
          setTimeout(function() {
            loadSpy.reset();
            target.dispatchEvent(new Event('load'));
            assert.isFalse(loadSpy.called, 'doesn\'t trigger the load event listener');
            done();
          });
        });

        Polymer.Base.importHref('dynamic-imports/async-import.html', loadSpy, errorSpy, true);
      });

      test('importHref does not leak event listeners on error', function(done) {
        var loadSpy = sinon.spy();
        var errorSpy = sinon.spy(function(e) {
          var target = e.target;
          target.dispatchEvent(new Event('load'));
          assert.isFalse(loadSpy.called, 'doesn\'t trigger the load event listener');
          setTimeout(function() {
            errorSpy.reset();
            target.dispatchEvent(new Event('error'));
            assert.isFalse(errorSpy.called, 'doesn\'t trigger the error event listener');
            done();
          });
        });

        Polymer.Base.importHref('dynamic-imports/async-import-invalid.html', loadSpy, errorSpy, true);
      });

    });

  });

</script>
</body>
</html>
